package com.damie.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.damie.common.dao.User;
import com.damie.common.dao.WasteOrder;
import com.damie.common.dao.WasteOrderDetails;
import com.damie.common.paramer.UserParamer;
import com.damie.web.mapper.UserMapper;
import com.damie.web.mapper.WasteOrderDetailsMapper;
import com.damie.web.mapper.WasteOrderMapper;
import com.damie.web.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ls
 * @since 2021-01-26
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private WasteOrderDetailsMapper wasteOrderDetailsMapper;
    @Autowired
    private WasteOrderMapper wasteOrderMapper;

    public User login(UserParamer userParamer){
        User user = new User();
        BeanUtils.copyProperties(userParamer,user);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userParamer.getUserId());
        queryWrapper.eq("user_password",userParamer.getUserPassword());
        User one = userMapper.selectOne(queryWrapper);
        return one;
    }


    public void addIntegral(String wasteOrderId){
        QueryWrapper<WasteOrderDetails> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("waste_order_id",wasteOrderId);
        List<WasteOrderDetails> wasteOrderDetails = wasteOrderDetailsMapper.selectList(queryWrapper);//根据订单号得到订单集合
        System.out.println(wasteOrderDetails);
        List<Double> wastePrices= wasteOrderDetails.stream().map(WasteOrderDetails::getWastePrice).collect(Collectors.toList());//筛选出订单集合中价格集合
        Double price = 0.00;
        for (Double p:wastePrices){//遍历相加
            price+=p;
        }
        price=price*10;//回收一元对应增加10积分
        System.out.println(price);

        QueryWrapper<WasteOrder> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("waste_order_id",wasteOrderId);
        WasteOrder wasteOrder = wasteOrderMapper.selectOne(queryWrapper1);
        String userId = wasteOrder.getUserId();//根据订单号得到userId
        System.out.println(userId);

        User user = userMapper.selectById(userId);//根据userId得到User对象
        double userIncome = user.getUserIncome();
        user.setUserIncome(price+userIncome);
        userMapper.updateById(user);
    }


}
